package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.queryobject.QueryObject;
import cn.wolfcode.resultpage.ResultPage;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class permissionServiceImpl implements IPermissionService{
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx; //Spring 上下文

    // 重新加载权限
    public void reload(){
        // 获取数据库中所有权限的表达式
        List<String> permissions = permissionMapper.selectAllExpression();

        // 1.获取所有的controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        // 2. 遍历每个controller,获取里面的每个方法
        Collection<Object> controllers = map.values();

        for (Object controller: controllers) {
            // 如果不是cglib代理对象,不往下执行
            if (!AopUtils.isCglibProxy(controller)){
                continue;
            }
            // controller上如果贴上了shiro的注解,shiro就会对该controller进行cglib(继承)代理
            // 这时候从容器中获取到的就是代理对象,代理类无法获取到父类的注解,所以先拿到父类,再从父类中获取
            Class clazz = controller.getClass().getSuperclass(); // controller的字节码对象
            Method[] methods = clazz.getDeclaredMethods(); //当前字节码中的方法
            // 3. 遍历每个方法,判断该方法上是否有贴自定义注解
            for (Method method : methods){
                // 获取方法上的RequiresPermissions标签
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if (annotation != null){
                    // 4.获取注解上的权限名称,以及权限表达式
                    String expression = annotation.value()[0];
                    // 判断数据库中是否存在用户的权限, String类型的contains()方法用来判断是否存在子字符串,结果反回true和false
                    if (!permissions.contains(expression)){ // 没有 --->添加
                        String name = annotation.value()[1];
                        //5.封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        // 6.把对象插入到数据中
                        permissionMapper.insert(permission);
                    }

                }
                // 没有---> 跳过
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectExpressionByEmpId(id);
    }

    @Override
    public int delete(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Permission permission) {
        return permissionMapper.insert(permission);
    }

    @Override
    public Permission selectOne(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public int update(Permission permission) {
        return permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo(permissions);

    }
}
